/**
	装载下拉框的角商品类型列表
 */
var searchForm = $(document.forms.searchForm);
var roleId = $(document.forms.searchForm.roleId);
$.get(
	"admin/user.let?action=listRoles",
	function(data){
		for(let role of data) {
			roleId.append(`<option value="${role.roleId}">${role.roleName}</option>`);
		}
	},"json"
);

//保存查询条件
var searchParams = new URLSearchParams();
searchForm.on("submit",function(){
	searchParams = sys.form.param(searchForm[0]);
	datagrid.bootstrapTable("refresh",{pageNumber:1});
	return false;
});


var datagrid = $("#datagrid").bootstrapTable({
	url: "admin/user.let?action=page",
	dataField: "list",//rows
	totalField: "total",
	queryParamsType: "",//limit
	pagination: true,
	sidePagination: "server",//client
	queryParams:function(params) { 
	  for(let name of searchParams.keys()){//添加搜索条件
		  console.info(name+"="+searchParams.get(name));
		  params[name]=searchParams.get(name);
	  }
	  console.info(params);
	  return params 
    }
});

var userSexFormatter = function(value, row, index, fieldName) {
	return value ? "男" : "女";
}

var indexFormatter = function(value, row, index, fieldName) {
	return index + 1;
}

var userStatusFormatter = function(v, row, index) {
	return `<input class="switch _userStatus" data-index="${index}" ${v == 1 ? "checked" : ""} type="checkbox">`;
}

/**
 * 格式化表格操作菜单
 */
var optionFormatter = function(value, row, index) {
	return `<div class="dropdown">
  <button class="btn btn-outline-primary btn-block dropdown-toggle" type="button" data-toggle="dropdown">
    操作
  </button>
  <div class="dropdown-menu dropdown-menu-right">
    <a class="dropdown-item "href="admin/user.let?action=findUser&userId=${value}">查看</a>
     <a class="dropdown-item "href="admin/user.let?action=updateUserForm&userId=${value}">修改</a>
    <a class="dropdown-item _updatePas" data-index="${index}" href="javascript:void(0);">重置登录密码</a>
    <a class="dropdown-item _delete" data-index="${index}" href="javascript:void(0);">删除</a>
  </div>
</div>`;
}

/**
	修改用户状态
 */
datagrid.on("change", "._userStatus", function() {
	let obj = $(this);
	let index = obj.data("index");//data-index
	let row = datagrid.bootstrapTable("getData")[index];
	let status = this.checked;
	$.post(
		"admin/user.let?action=updateStatus",
		{ "userId": row.userId, userStatus: status ? 1 : 0 },
		function(data) {
			if (data.code == 200) {
				if (status) {
					sys.toastr.warn(`用户[${row.userNickname}]被锁定了`);
				} else {
					sys.toastr.success(`用户[${row.userNickname}]解锁成功`);
				}

			} else {
				sys.toastr.error(data.message);
				obj[0].checked = !obj[0].checked;
			}
		}, "json"
	);

}).on("click", "._delete", function() {//删除用户
	let obj = $(this);
	let index = obj.data("index");//data-index
	let row = datagrid.bootstrapTable("getData")[index];
	sys.confirm(`您确定要删除[${row.userNickname}]吗？`, function(r) {
		if (r) {
			$.post(
				"admin/user.let?action=delete",
				{ "userId": row.userId },
				function(data) {
					if (data.code == 200) {
						sys.toastr.success(`删除用户[${row.userNickname}]成功`);
						datagrid.bootstrapTable("refresh");
					} else {
						sys.toastr.error(data.message);
					}
				}, "json"
			);
		}
	});
}).on("click", "._updatePas", function() {//重置用户密码
	let obj = $(this);
	let index = obj.data("index");//data-index
	let row = datagrid.bootstrapTable("getData")[index];
	sys.confirm(`您确定要重置[${row.userNickname}]密码吗？`, function(r) {
		if (r) {
			$.post(
				"admin/user.let?action=updatePassw",
				{ "userId": row.userId },
				function(data) {
					if (data.code == 200) {
						sys.toastr.success(`重置用户[${row.userNickname}]密码成功`);
						datagrid.bootstrapTable("refresh");
					} else {
						sys.toastr.error(data.message);
					}
				}, "json"
			);
		}
	});
});
